﻿@page "/charts/line/basic"
@using ChartJs.Blazor.LineChart
@layout SampleLayout

<Chart Config="_config" @ref="_chart"></Chart>

<button @onclick="RandomizeData">Randomize Data</button>
<button @onclick="AddDataset">Add Dataset</button>
<button @onclick="RemoveDataset">Remove Dataset</button>
<button @onclick="AddData">Add Data</button>
<button @onclick="RemoveData">Remove Data</button>

@code {
    private const int InitalCount = 7;
    private LineConfig _config;
    private Chart _chart;

    protected override void OnInitialized()
    {
        _config = new LineConfig
        {
            Options = new LineOptions
            {
                Responsive = true,
                Title = new OptionsTitle
                {
                    Display = true,
                    Text = "ChartJs.Blazor Line Chart"
                },
                Tooltips = new Tooltips
                {
                    Mode = InteractionMode.Nearest,
                    Intersect = true
                },
                Hover = new Hover
                {
                    Mode = InteractionMode.Nearest,
                    Intersect = true
                },
                Scales = new Scales
                {
                    XAxes = new List<CartesianAxis>
                    {
                        new CategoryAxis
                        {
                            ScaleLabel = new ScaleLabel
                            {
                                LabelString = "Month"
                            }
                        }
                    },
                    YAxes = new List<CartesianAxis>
                    {
                        new LinearCartesianAxis
                        {
                            ScaleLabel = new ScaleLabel
                            {
                                LabelString = "Value"
                            }
                        }
                    }
                }
            }
        };

        IDataset<int> dataset1 = new LineDataset<int>(RandomScalingFactor(InitalCount))
        {
            Label = "My first dataset",
            BackgroundColor = ColorUtil.FromDrawingColor(ChartColors.Red),
            BorderColor = ColorUtil.FromDrawingColor(ChartColors.Red),
            Fill = FillingMode.Disabled
        };

        IDataset<int> dataset2 = new LineDataset<int>(RandomScalingFactor(InitalCount))
        {
            Label = "My second dataset",
            BackgroundColor = ColorUtil.FromDrawingColor(ChartColors.Blue),
            BorderColor = ColorUtil.FromDrawingColor(ChartColors.Blue),
            Fill = FillingMode.Disabled
        };

        _config.Data.Labels.AddRange(Months.Take(InitalCount));
        _config.Data.Datasets.Add(dataset1);
        _config.Data.Datasets.Add(dataset2);
    }

    private void RandomizeData()
    {
        foreach (IDataset<int> dataset in _config.Data.Datasets)
        {
            int count = dataset.Count;
            dataset.Clear();
            dataset.AddRange(RandomScalingFactor(count));
        }

        _chart.Update();
    }

    private void AddDataset()
    {
        string color = ColorUtil.FromDrawingColor(ChartColors.All[_config.Data.Datasets.Count % ChartColors.All.Count]);
        IDataset<int> dataset = new LineDataset<int>(RandomScalingFactor(_config.Data.Labels.Count))
        {
            Label = $"Dataset {_config.Data.Datasets.Count}",
            BackgroundColor = color,
            BorderColor = color,
            Fill = FillingMode.Disabled
        };

        _config.Data.Datasets.Add(dataset);
        _chart.Update();
    }

    private void RemoveDataset()
    {
        IList<IDataset> datasets = _config.Data.Datasets;
        if (datasets.Count == 0)
            return;

        datasets.RemoveAt(datasets.Count - 1);
        _chart.Update();
    }

    private void AddData()
    {
        if (_config.Data.Datasets.Count == 0)
            return;

        string month = Months[_config.Data.Labels.Count % Months.Count];
        _config.Data.Labels.Add(month);

        foreach (IDataset<int> dataset in _config.Data.Datasets)
        {
            dataset.Add(RandomScalingFactor());
        }

        _chart.Update();
    }

    private void RemoveData()
    {
        if (_config.Data.Datasets.Count == 0 ||
            _config.Data.Labels.Count == 0)
        {
            return;
        }

        _config.Data.Labels.RemoveAt(_config.Data.Labels.Count - 1);

        foreach (IDataset<int> dataset in _config.Data.Datasets)
        {
            dataset.RemoveAt(dataset.Count - 1);
        }

        _chart.Update();
    }
}
